﻿Imports System.IO

Public Class frmMain
    Private lPath As String = "\My Documents\mm_Data\"
    Private lFileName As String = "ActoionItems.xml"

    Sub SaveData()
        If Not Directory.Exists(lPath) Then _
            Directory.CreateDirectory(lPath)
        DsActionItems.ActionItems.AcceptChanges()
        DsActionItems.WriteXml(lPath & lFileName)
    End Sub

    Sub StatusUpdate()
        StatusBar1.Text = "Rows " & ActionItemsBindingSource.Count.ToString
    End Sub

    Sub LoadData()
        Try

            If File.Exists(lPath & lFileName) Then
                DsActionItems.ReadXml(lPath & lFileName)
            End If

            Me.Text = "Meeting Mate " & lFileName
            For Each dr As dsActionItems.ActionItemsRow In DsActionItems.ActionItems.Rows
                Try
                    dr.StartDate = FormatDateTime(dr.StartDate, DateFormat.ShortDate)
                Catch ex As Exception
                End Try
                Try
                    dr.ActionDate = FormatDateTime(dr.ActionDate, DateFormat.ShortDate)
                Catch ex As Exception
                End Try
            Next
            StatusUpdate()
            If DsActionItems.Status.Rows.Count = 0 Then
                Dim stat() As String = {"New", "Pending", "On Hold", "Closed"}
                Dim dr As dsActionItems.StatusRow
                For Each Str As String In stat
                    dr = DsActionItems.Status.NewStatusRow
                    dr.Status = Str
                    DsActionItems.Status.AddStatusRow(dr)
                Next
            End If

            If DsActionItems.Privacy.Rows.Count = 0 Then
                Dim Privacy() As String = {"Public ", "Privacy 1", "Privacy 2", "Privacy 3"}
                Dim dr As dsActionItems.PrivacyRow
                For Each Str As String In Privacy
                    dr = DsActionItems.Privacy.NewPrivacyRow
                    dr.Privacy = Str
                    DsActionItems.Privacy.AddPrivacyRow(dr)
                Next
            End If

            If DsActionItems.Catagories.Rows.Count = 0 Then
                Dim Catagories() As String = {"Production Support", "Meeting", "My Action", "Other"}
                Dim dr As dsActionItems.CatagoriesRow
                For Each Str As String In Catagories
                    dr = DsActionItems.Catagories.NewCatagoriesRow
                    dr.Catagory = Str
                    DsActionItems.Catagories.AddCatagoriesRow(dr)
                Next
            End If

            If DsActionItems.Person.Rows.Count = 0 Then
                Dim Person() As String = {"Pat", "Greg", "Deon", "Other"}
                Dim dr As dsActionItems.PersonRow
                For Each Str As String In Person
                    dr = DsActionItems.Person.NewPersonRow
                    dr.PersonName = Str
                    DsActionItems.Person.AddPersonRow(dr)
                Next
            End If
            BindFilter
        Catch ex As Exception
            BasicError(ex)
        End Try

    End Sub

    Sub BindFilter()

        chkFilterCatagory.DataSource = DsActionItems.Catagories
        chkFilterCatagory.DisplayMember = "Catagory"
        chkFilterCatagory.ValueMember = "CatagoryID"
        chkFilterCatagory.ParentIDMember = "ItemID"
        chkFilterCatagory.dtRelationDataSource = DsActionItems.Item_Catagory
        chkFilterCatagory.BindData()

        chkFilterPerson.DataSource = DsActionItems.Person
        chkFilterPerson.DisplayMember = "PersonName"
        chkFilterPerson.ValueMember = "PersonID"
        chkFilterPerson.ParentIDMember = "ItemID"
        chkFilterPerson.dtRelationDataSource = DsActionItems.Person_Item
        chkFilterPerson.BindData()

        chkFilterStatus.DataSource = DsActionItems.Status
        chkFilterStatus.DisplayMember = "Status"
        chkFilterStatus.ValueMember = "StatusID"
        chkFilterStatus.ParentIDMember = "ItemID"
        chkFilterStatus.dtRelationDataSource = DsActionItems.ActionItems
        chkFilterStatus.BindData()

    End Sub

    Sub BasicError(ByVal pex As Exception)
        MessageBox.Show(pex.Message)
    End Sub

    Sub AddNewItem()
        Dim dr As dsActionItems.ActionItemsRow = Me.DsActionItems.ActionItems.NewActionItemsRow
        dr.StatusID = -1
        dr.PrivacyID = -1
        Me.DsActionItems.ActionItems.AddActionItemsRow(dr)
        ActionItemsBindingSource.MoveLast()
        Dim ActionItemsEditViewDialog As MM_PDA.ActionItemsEditViewDialog = MM_PDA.ActionItemsEditViewDialog.Instance(Me.ActionItemsBindingSource)
        ActionItemsEditViewDialog.ShowDialog()
        StatusUpdate()
    End Sub

    Private Sub dgSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgSummary.Click
        
        Dim ActionItemsEditViewDialog As MM_PDA.ActionItemsEditViewDialog = MM_PDA.ActionItemsEditViewDialog.Instance(Me.ActionItemsBindingSource)
        ActionItemsEditViewDialog.ShowDialog()

    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub

    Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
        SaveData()
    End Sub

    Private Sub mnuNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNew.Click
        AddNewItem()
    End Sub

    Private Sub chkFilterCatagory_FilterChanged(ByVal Filter As String) Handles chkFilterCatagory.FilterChanged
        Dim sFilter As String = chkFilterPerson.Filter

        If sFilter.Length > 0 And Filter.Length > 0 Then
            ActionItemsBindingSource.Filter = sFilter & " AND " & Filter
        ElseIf sFilter.Length > 0 And Filter.Length = 0 Then
            ActionItemsBindingSource.Filter = sFilter
        Else
            ActionItemsBindingSource.Filter = Filter
        End If

    End Sub

    Private Sub chkFilterPerson_FilterChanged(ByVal Filter As String) Handles chkFilterPerson.FilterChanged
        Dim sFilter As String = chkFilterCatagory.Filter
        If sFilter.Length > 0 And Filter.Length > 0 Then
            ActionItemsBindingSource.Filter = sFilter & " AND " & Filter
        ElseIf sFilter.Length > 0 And Filter.Length = 0 Then
            ActionItemsBindingSource.Filter = sFilter
        Else
            ActionItemsBindingSource.Filter = Filter
        End If
    End Sub

    Private Sub chkFilterStatus_FilterChanged(ByVal Filter As String) Handles chkFilterStatus.FilterChanged
        Debug.WriteLine(Filter)
    End Sub
End Class
